﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CodeStash.Common.DataAccess.Repository;
using CodeStash.Common.DataAccess.EntityFramework;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace CodeStash.IntegrationTests
{
    [TestFixture]
    public class CodeCategoryTests
    {
        [Test]
        public void SimpleInsertAndDeleteTest()
        {
            Repository<CodeCategory> codeCategoryRepository = new Repository<CodeCategory>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                codeCategoryRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                codeCategoryRepository.Add(new CodeCategory(uniqueId.ToString()));
                db.SaveChanges();

                IEnumerable<CodeCategory> categories = codeCategoryRepository.FindBy(x => x.CodeCategoryName == uniqueId.ToString());
                Assert.AreEqual(1, categories.Count());

                codeCategoryRepository.Remove(categories.First());
                db.SaveChanges();

                categories = codeCategoryRepository.FindBy(x => x.CodeCategoryName == uniqueId.ToString());
                Assert.AreEqual(0, categories.Count());
            }
        }

        [Test]
        public void FetchAllLazyLoadingSpeficCategory()
        {
            Repository<CodeCategory> codeCategoryRepository = new Repository<CodeCategory>();
 
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                codeCategoryRepository.EnrolInUnitOfWork(db);
                var categories1 = codeCategoryRepository.FindAll("CodeSnippets").ToList();
                Assert.GreaterOrEqual(categories1.Count(),1);
            }
        }

    }
}
